#include "checkmedevice.h"
#include <QDebug>
#include <QDir>

checkmeDevice::checkmeDevice(QObject *parent) : QObject(parent)
{

}

checkmeDevice::~checkmeDevice()
{

}

checkmeDevice::StateType checkmeDevice::connectDevice()
{
    return deviceState;
}

checkmeDevice::StateType checkmeDevice::pingDevice()
{
    return deviceState;
}

checkmeDevice::StateType checkmeDevice::disconnectDevice()
{
    return deviceState;
}

void checkmeDevice::downloadAllFile()
{
    qDebug()<<"device threadID:"<<QThread::currentThreadId();

    qDebug()<<name;
    xdumper.getFileList(listItemCallback);
    qDebug()<<fileNameList;

    QString dirStr("./" + name);
    if(!fileNameList.isEmpty()){
        QDir *temp = new QDir;
        bool exist = temp->exists(dirStr);
        if(!exist){
            bool ok = temp->mkdir(dirStr);
        }
    }else{
//        QMessageBox::warning(this, "Download Faild", "no File!");
        return;
    }
    dirStr += "/";
    for(int i = 0; i < fileNameList.size(); i++){
        QString localFilePath = dirStr + fileNameList.at(i);
        QByteArray remoteBA = fileNameList.at(i).toLatin1();
        char *remote = remoteBA.data();
        QByteArray loaclBA = localFilePath.toLatin1();
        char *local = loaclBA.data();
        xdumper.getFile(remote, local, NULL, NULL, NULL);
    }
    fileNameList.clear();

//    delete this;
}

QStringList checkmeDevice::fileNameList;
int __stdcall checkmeDevice::listItemCallback(int cmd, int idx, const char *item, void *arglist)
{
    Q_UNUSED(idx);
    Q_UNUSED(arglist);

    switch (cmd) {
    case CB_CMD_START:
        break;

    case CB_CMD_DOING:{
        if(!item)
            return(-1);

        QString getstring(item);
        QString fileName = getstring.section(" ",0,0);
        fileNameList<<fileName;
        break;
    }

    case CB_CMD_END:
        break;
    }

    return(0);
}
